import Vue from 'vue';
import Vuex from 'vuex';
import user from './modules/user';
import distribution from './modules/distribution';
import goods from './modules/goods';
import order from './modules/order';

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    // 全局状态
    isLoading: false,
    systemInfo: null,
    location: null
  },
  
  mutations: {
    // 全局mutations
    SET_LOADING(state, loading) {
      state.isLoading = loading;
    },
    
    SET_SYSTEM_INFO(state, info) {
      state.systemInfo = info;
    },
    
    SET_LOCATION(state, location) {
      state.location = location;
    }
  },
  
  actions: {
    // 全局actions
    setLoading({ commit }, loading) {
      commit('SET_LOADING', loading);
    },
    
    async initSystemInfo({ commit }) {
      try {
        const systemInfo = await uni.getSystemInfo();
        commit('SET_SYSTEM_INFO', systemInfo);
        return systemInfo;
      } catch (error) {
        console.error('获取系统信息失败:', error);
      }
    },
    
    async getCurrentLocation({ commit }) {
      try {
        const location = await uni.getLocation({
          type: 'wgs84'
        });
        commit('SET_LOCATION', location);
        return location;
      } catch (error) {
        console.error('获取位置信息失败:', error);
      }
    }
  },
  
  getters: {
    // 全局getters
    isLoading: state => state.isLoading,
    systemInfo: state => state.systemInfo,
    location: state => state.location
  },
  
  modules: {
    user,
    distribution,
    goods,
    order
  }
});

export default store;